#include "stdafx.h"
#include "Diagnostics.h"
#include <sstream>
#include "unicode.h"
#include <iomanip>

void dbgPrint(TCHAR* format, ...)
{
	va_list args;
	int len;
	TCHAR * buffer;

	va_start( args, format );
	len = _vsctprintf( format, args ) + 1;// _vscprintf doesn't count terminating '\0'
	buffer = new TCHAR[len];

	try
	{
		_vstprintf( buffer, format, args );
		OutputDebugString(buffer);
	}
	catch (...)
	{
		//Just so that we do not leak memory (i.e. the buffer
		//is deleted even when vsprintf throws an exception)
	}

	delete buffer;
}

std::_tstring guidToString(const GUID& guid)
{
	//23020D1D-7050-4694-B6C7-DA5563C49955
	TCHAR buff[sizeof(GUID)*2+4+1];//one byte = 2 hexits + 4 hyphens + NULL terminator

	TCHAR* curPos = buff;
	
	for(int i=0; i<sizeof(GUID); i++)
	{
		unsigned int byteVal = ((unsigned char*)&guid)[i];

		int len=0;
		if(i == 3 || i == 5 || i == 7 || i == 9)
			len = _stprintf(curPos, _T("%02X-"), byteVal);
		else
			len = _stprintf(curPos, _T("%02X"), byteVal);
		curPos += len;
	}

	*curPos=_T('\0');

	return buff;
}